package com.example.androidaudiovideodemo

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

/**
 *
 * author: renbing
 *
 * date: 2021/12/15
 *
 * des:
 */
class AudioListAdapter<T>(private val context: Context, private val datas: List<T>, private val itemClick: (position: Int, data: T) -> Unit,private val itemCheck: (position: Int, data: T) -> Unit) : RecyclerView.Adapter<AudioListAdapter<T>.ViewHolder>(){

    inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
        val name = itemView.findViewById<TextView>(R.id.item_name)
        val size = itemView.findViewById<TextView>(R.id.item_size)
        val time = itemView.findViewById<TextView>(R.id.item_time)
        val duration = itemView.findViewById<TextView>(R.id.item_duration)
        val line = itemView.findViewById<View>(R.id.item_line)
        val select = itemView.findViewById<CheckBox>(R.id.item_select)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_audio_list,parent,false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.name.text = (datas[position] as AudioListBean).name
        holder.size.text = (datas[position] as AudioListBean).size
        holder.time.text = (datas[position] as AudioListBean).time
        holder.duration.text = (datas[position] as AudioListBean).duration
        holder.select.isChecked = (datas[position] as AudioListBean).isSelect

        holder.line.visibility = if (position == datas.size-1) View.GONE else View.VISIBLE

        holder.itemView.setOnClickListener {
            itemClick.invoke(position,datas[position])
        }

        holder.select.setOnClickListener {
            itemCheck.invoke(position,datas[position])
        }
    }

    override fun getItemCount(): Int {
        return datas.size
    }
}